package com.lg.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.common.pojo.ResponseResult;
import com.lg.product.entity.Sku;
import com.lg.product.entity.Stock;
import com.lg.product.service.ISkuService;
import com.lg.product.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * sku表,该表表示具体的商品实体,如黑色的 64g的iphone 8 前端控制器
 * </p>
 *
 * @author lzk
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Autowired
    ISkuService iSkuService;

    @Autowired
    IStockService iStockService;

    @RequestMapping("list")
    public ResponseResult list(Sku sku, Page page){

        return ResponseResult.success(iSkuService.selectSKuPage(page, sku));
    }

    @RequestMapping("/updateEnable")
    public ResponseResult updateEnable(@RequestBody Sku sku){
        System.out.println(sku+"@@@@@@@@@@@@@@@");
        return ResponseResult.success(iSkuService.updateById(sku));
    }

    @RequestMapping("/editSku")
    public ResponseResult editSku(@RequestBody Sku sku){
        System.out.println(sku+"@@@@@@@@@@@@@@@");
        sku.setLastUpdateTime(new Date());
        Stock stock = new Stock();
        QueryWrapper queryWrapper = new QueryWrapper();
        stock.setStock(sku.getStock());
        queryWrapper.eq("sku_id",sku.getId());
        iStockService.update(stock, queryWrapper);
        return ResponseResult.success(iSkuService.updateById(sku));
    }

}
